// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2016-2018, LomboTech Co.Ltd.
 * Authors:
 *	lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/backlight.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>

#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_mipi_dsi.h>
#include <drm/drm_panel.h>
#include <video/display_timing.h>
#include <video/of_display_timing.h>
#include <video/videomode.h>

#define T1_WIDTH	(320)
#define T1_HEIGHT	(1280)
#define T1_HBP		(60)
#define T1_HFP		(50)
#define T1_HSW		(30)
#define T1_HACT		(T1_WIDTH)
#define T1_VBP_F1	(30)
#define T1_VFP_F1	(20)
#define T1_VSW_F1	(15)
#define T1_VACT_F1	(T1_HEIGHT)
#define T1_VT_F1	(T1_VBP_F1 + T1_VFP_F1 + T1_VSW_F1 + T1_VACT_F1)
#define T1_DCLK		(16666000)

#define DCS_MDELAY_FLAG (0)
#define LANE_NUM	(2)

struct panel_wtl_desc {
	const struct drm_display_mode *modes;
	unsigned int num_modes;

	unsigned int bpc;
	struct {
		unsigned int width;
		unsigned int height;
	} size;

	/**
	 * @prepare: the time (in milliseconds) that it takes for the panel to
	 *           become ready and start receiving video data
	 * @enable: the time (in milliseconds) that it takes for the panel to
	 *          display the first valid frame after starting to receive
	 *          video data
	 * @disable: the time (in milliseconds) that it takes for the panel to
	 *           turn the display off (no content is visible)
	 * @unprepare: the time (in milliseconds) that it takes for the panel
	 *             to power itself down completely
	 */
	struct {
		unsigned int prepare;
		unsigned int enable;
		unsigned int disable;
		unsigned int unprepare;
	} delay;

	u32 bus_format;
	unsigned long flag;
	unsigned int lanes;
	enum mipi_dsi_pixel_format format;
};

struct panel_wtl {
	struct drm_panel base;
	bool prepared;
	bool enabled;

	struct device *dev;
	struct display_timings *timings;
	const struct panel_wtl_desc *desc;

	struct backlight_device *backlight;
	struct regulator *supply;

	int reset_gpio;
	int power_gpio;
	u32 modes_idx;
};

static u8 wtl_init_code[] = {
	2, 0xB0, 0x5A,

	2, 0xB1, 0x00,
	2, 0x89, 0x01,
	2, 0x91, 0x17,
	2, 0xB1, 0x03,
	2, 0x2C, 0x28,

	2, 0x00, 0xF1,
	2, 0x01, 0x18,
	2, 0x02, 0x00,
	2, 0x03, 0x00,
	2, 0x04, 0x00,
	2, 0x05, 0x00,
	2, 0x06, 0x00,
	2, 0x07, 0x00,
	2, 0x08, 0x00,
	2, 0x09, 0x00,
	2, 0x0A, 0x01,
	2, 0x0B, 0x01,
	2, 0x0C, 0x00,
	2, 0x0D, 0x00,
	2, 0x0E, 0x24,
	2, 0x0F, 0x1C,
	2, 0x10, 0xC9,
	2, 0x11, 0x60,
	2, 0x12, 0x70,
	2, 0x13, 0x01,
	2, 0x14, 0xE5,
	2, 0x15, 0xFF,
	2, 0x16, 0x3D,
	2, 0x17, 0x0E,
	2, 0x18, 0x01,
	2, 0x19, 0x00,
	2, 0x1A, 0x00,
	2, 0x1B, 0xFC,
	2, 0x1C, 0x0B,
	2, 0x1D, 0xA0,
	2, 0x1E, 0x03,
	2, 0x1F, 0x04,
	2, 0x20, 0x0C,
	2, 0x21, 0x00,
	2, 0x22, 0x04,
	2, 0x23, 0x81,
	2, 0x24, 0x1F,
	2, 0x25, 0x10,
	2, 0x26, 0x9B,
	2, 0x2D, 0x01,
	2, 0x2E, 0x84,
	2, 0x2F, 0x00,
	2, 0x30, 0x02,
	2, 0x31, 0x08,
	2, 0x32, 0x01,
	2, 0x33, 0x1C,
	2, 0x34, 0x40,
	2, 0x35, 0xFF,
	2, 0x36, 0xFF,
	2, 0x37, 0xFF,
	2, 0x38, 0xFF,
	2, 0x39, 0xFF,
	2, 0x3A, 0x05,
	2, 0x3B, 0x00,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0,
	2, 0x4E, 0x0F,
	2, 0x4F, 0x61,
	2, 0x50, 0x78,
	2, 0x51, 0x7A,
	2, 0x52, 0x34,
	2, 0x53, 0x99,
	2, 0x54, 0xA2,
	2, 0x55, 0x02,
	2, 0x56, 0x14,
	2, 0x57, 0xB8,
	2, 0x58, 0xDC,
	2, 0x59, 0x34,
	2, 0x5A, 0x1E,
	2, 0x5B, 0x8F,
	2, 0x5C, 0xC7,
	2, 0x5D, 0xE3,
	2, 0x5E, 0xF1,
	2, 0x5F, 0x78,
	2, 0x60, 0x3C,
	2, 0x61, 0x36,
	2, 0x62, 0x1E,
	2, 0x63, 0x1B,
	2, 0x64, 0x8F,
	2, 0x65, 0xC7,
	2, 0x66, 0xE3,
	2, 0x67, 0x31,
	2, 0x68, 0x0C,
	2, 0x69, 0x89,
	2, 0x6A, 0x30,
	2, 0x6B, 0x8C,
	2, 0x6C, 0x8D,
	2, 0x6D, 0x8D,
	2, 0x6E, 0x8D,
	2, 0x6F, 0x8D,
	2, 0x70, 0xC7,
	2, 0x71, 0xE3,
	2, 0x72, 0x31,
	2, 0x73, 0x00,
	2, 0x74, 0x00,
	2, 0x75, 0x00,
	2, 0x76, 0x00,
	2, 0x77, 0x00,
	2, 0x78, 0x00,
	2, 0x79, 0x00,
	2, 0x7A, 0xC6,
	2, 0x7B, 0xC6,
	2, 0x7C, 0xC6,
	2, 0x7D, 0xC6,
	2, 0x7E, 0xC6,
	2, 0x7F, 0xE3,
	2, 0x0B, 0x00,

	2, 0xB1, 0x03,
	2, 0x2C, 0x2C,

	2, 0xB1, 0x00,
	2, 0x89, 0x03,
};

static int panel_wtl_mipi_dsi_dcs_write_array(
	struct mipi_dsi_device *dsi, const u8 *ini_code, size_t len)
{
	u32 i = 0;

	while (i < len) {
		if (ini_code[i] == DCS_MDELAY_FLAG) {
			mdelay(ini_code[i + 1]);
			i += 2;
		} else {
			DRM_DEBUG_KMS("%x, %x\n", ini_code[i + 1],
					ini_code[i + 2]);
			mipi_dsi_dcs_write_buffer(dsi,
				(const void *)&ini_code[i + 1], ini_code[i]);
			i += ini_code[i] + 1;
		}
	}
	return 0;
}

static inline struct panel_wtl *to_panel_wtl(struct drm_panel *panel)
{
	return container_of(panel, struct panel_wtl, base);
}

static int panel_wtl_disable(struct drm_panel *panel)
{
	struct panel_wtl *p = to_panel_wtl(panel);

	DRM_DEBUG_KMS("[%d] p->enabled:%d\n", __LINE__, p->enabled);

	if (!p->enabled)
		return 0;

	if (p->backlight) {
		p->backlight->props.power = FB_BLANK_POWERDOWN;
		p->backlight->props.state |= BL_CORE_FBBLANK;
		backlight_update_status(p->backlight);
	}

	if (p->desc && p->desc->delay.disable)
		msleep(p->desc->delay.disable);

	p->enabled = false;

	return 0;
}

static int panel_wtl_unprepare(struct drm_panel *panel)
{
	struct panel_wtl *p = to_panel_wtl(panel);

	DRM_DEBUG_KMS("[%d] p->prepared:%d\n", __LINE__, p->prepared);

	if (!p->prepared)
		return 0;

	if (p->reset_gpio)
		gpio_direction_output(p->reset_gpio, 0);

	if (p->power_gpio)
		gpio_direction_output(p->power_gpio, 0);

	if (p->supply)
		regulator_disable(p->supply);

	if (p->desc && p->desc->delay.unprepare)
		msleep(p->desc->delay.unprepare);

	p->prepared = false;

	return 0;
}

static int panel_wtl_prepare(struct drm_panel *panel)
{
	struct panel_wtl *p = to_panel_wtl(panel);
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(panel->dev);
	int err;

	DRM_DEBUG_KMS("[%d] p->prepared:%d\n", __LINE__, p->prepared);

	if (p->prepared)
		return 0;
	p->prepared = true;

	if (p->supply) {
		err = regulator_enable(p->supply);
		if (err < 0) {
			dev_err(panel->dev,
				"failed to enable supply: %d\n", err);
			return err;
		}
	}

	if (p->power_gpio) {
		gpio_direction_output(p->power_gpio, 1);
		msleep(100);
	}

	if (p->reset_gpio) {
		gpio_direction_output(p->reset_gpio, 1);
		msleep(100);
		gpio_direction_output(p->reset_gpio, 0);
		msleep(100);
		gpio_direction_output(p->reset_gpio, 1);
		msleep(100);
	}

	if (p->desc && p->desc->delay.prepare)
		msleep(p->desc->delay.prepare);

	panel_wtl_mipi_dsi_dcs_write_array(dsi,
		wtl_init_code, ARRAY_SIZE(wtl_init_code));

	return 0;
}

static int panel_wtl_enable(struct drm_panel *panel)
{
	struct panel_wtl *p = to_panel_wtl(panel);

	DRM_DEBUG_KMS("[%d] p->enabled:%d\n", __LINE__, p->enabled);

	if (p->enabled)
		return 0;
	p->enabled = true;

	if (p->desc && p->desc->delay.enable)
		msleep(p->desc->delay.enable);

	if (p->backlight) {
		p->backlight->props.state &= ~BL_CORE_FBBLANK;
		p->backlight->props.power = FB_BLANK_UNBLANK;
		backlight_update_status(p->backlight);
	}

	return 0;
}

static int panel_wtl_get_fixed_modes(
	struct panel_wtl *panel)
{
	struct drm_connector *connector = panel->base.connector;
	struct drm_device *drm = panel->base.drm;
	struct drm_display_mode *mode;
	unsigned int i, num = 0;

	if (!panel->desc)
		return 0;

	if (panel->timings)
		for (i = 0; i < panel->timings->num_timings; i++) {
			const struct display_timing *dt =
					panel->timings->timings[i];
			struct videomode vm;

			videomode_from_timing(dt, &vm);
			mode = drm_mode_create(drm);
			if (!mode) {
				dev_err(drm->dev, "failed to add mode %ux%u\n",
					dt->hactive.typ, dt->vactive.typ);
				continue;
			}
			drm_display_mode_from_videomode(&vm, mode);
			if (i == panel->timings->native_mode)
				mode->type |= DRM_MODE_TYPE_PREFERRED;
			drm_mode_probed_add(connector, mode);
			num++;
		}

	for (i = 0; i < panel->desc->num_modes; i++) {
		const struct drm_display_mode *m =
				panel->desc->modes + i;

		mode = drm_mode_duplicate(drm, m);
		if (!mode) {
			DRM_ERROR("failed to add mode %ux%u@%u\n",
				m->hdisplay, m->vdisplay, m->vrefresh);
			continue;
		}
		drm_mode_set_name(mode);
		drm_mode_probed_add(connector, mode);
		num++;
	}

	connector->display_info.bpc = panel->desc->bpc;
	connector->display_info.width_mm = panel->desc->size.width;
	connector->display_info.height_mm = panel->desc->size.height;

	return num;
}

static int panel_wtl_get_modes(struct drm_panel *panel)
{
	struct panel_wtl *p = to_panel_wtl(panel);
	int num = 0;

	DRM_DEBUG_KMS("[%d] p->enabled:%d\n", __LINE__, p->enabled);

	/* add hard-coded panel modes */
	num += panel_wtl_get_fixed_modes(p);

	return num;
}

static int panel_wtl_get_timings(struct drm_panel *panel,
				    unsigned int num_timings,
				    struct display_timing *timings)
{
	DRM_DEBUG_KMS("%s,%d\n", __func__, __LINE__);
	return 0;
}

static const struct drm_panel_funcs panel_wtl_funcs = {
	.disable = panel_wtl_disable,
	.unprepare = panel_wtl_unprepare,
	.prepare = panel_wtl_prepare,
	.enable = panel_wtl_enable,
	.get_modes = panel_wtl_get_modes,
	.get_timings = panel_wtl_get_timings,
};

static int panel_wtl_probe(struct device *dev,
			       const struct panel_wtl_desc *desc)
{
	struct device_node *np = dev->of_node;
	struct panel_wtl *panel;
	enum of_gpio_flags flags;
	int err;
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);

	dev_dbg(dev, "%s,%d\n", __FILE__, __LINE__);

	panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
	if (!panel)
		return -ENOMEM;

	panel->power_gpio = of_get_named_gpio_flags(
		np, "power_gpio", 0, &flags);
	DRM_DEBUG_KMS("power_gpio %d, flags 0x%x\n", panel->power_gpio, flags);
	if (!gpio_is_valid(panel->power_gpio)) {
		DRM_ERROR("power_gpio %d invalid\n", panel->power_gpio);
		panel->power_gpio = 0;
	} else {
		err = devm_gpio_request(dev, panel->power_gpio, "power_gpio");
		if (err < 0) {
			DRM_ERROR("power_gpio %d request failed\n",
				panel->power_gpio);
			panel->power_gpio = 0;
		}
	}

	panel->reset_gpio = of_get_named_gpio_flags(
		np, "reset_gpio", 0, &flags);
	DRM_DEBUG_KMS("reset_gpio %d, flags 0x%x\n", panel->reset_gpio, flags);
	if (!gpio_is_valid(panel->reset_gpio)) {
		DRM_ERROR("reset_gpio %d invalid\n", panel->reset_gpio);
		panel->reset_gpio = 0;
	} else {
		err = devm_gpio_request(dev, panel->reset_gpio, "reset_gpio");
		if (err < 0) {
			DRM_ERROR("power_gpio %d request failed\n",
				panel->reset_gpio);
			panel->reset_gpio = 0;
		}
	}

	panel->timings = of_get_display_timings(np);

	dsi->lanes = desc->lanes;
	dsi->format = desc->format;
	dsi->mode_flags = desc->flag;

	panel->desc = desc;
	panel->dev = dev;

	drm_panel_init(&panel->base);
	panel->base.dev = dev;
	panel->base.funcs = &panel_wtl_funcs;

	err = drm_panel_add(&panel->base);
	if (err < 0)
		goto err_out;

	mipi_dsi_attach(dsi);
	if (err < 0)
		goto err_out;

	dev_set_drvdata(dev, panel);

	DRM_DEBUG_KMS("[%d] over.\n", __LINE__);

	return 0;

err_out:
	return err;
}

static int panel_wtl_remove(struct mipi_dsi_device *dsi)
{
	struct panel_wtl *panel = dev_get_drvdata(&dsi->dev);

	if (panel->timings)
		display_timings_release(panel->timings);

	mipi_dsi_detach(dsi);
	drm_panel_detach(&panel->base);
	drm_panel_remove(&panel->base);

	panel_wtl_disable(&panel->base);

	if (panel->backlight)
		put_device(&panel->backlight->dev);

	return 0;
}

static void panel_wtl_shutdown(struct mipi_dsi_device *dsi)
{
	struct panel_wtl *panel = dev_get_drvdata(&dsi->dev);

	panel_wtl_disable(&panel->base);
}

static const struct drm_display_mode wtl_mode = {
	/* dclk_freq */
	.clock = T1_DCLK / 1000,
	/* width */
	.hdisplay = T1_WIDTH,
	/* hsync_start = hdisplay + hfp */
	.hsync_start = T1_WIDTH + T1_HFP,
	/* hsync_end = hdisplay + hfp + hsw */
	.hsync_end = T1_WIDTH + T1_HFP + T1_HSW,
	/* htotal = hdisplay + hfp + hsw + hbp */
	.htotal = T1_WIDTH + T1_HFP + T1_HSW + T1_HBP,
	/* height */
	.vdisplay = T1_HEIGHT,
	/* vsync_start = vdisplay + vfp */
	.vsync_start = T1_HEIGHT + T1_VFP_F1,
	/* vsync_end = vdisplay + vfp + vsw */
	.vsync_end = T1_HEIGHT + T1_VFP_F1 + T1_VSW_F1,
	/* vtotal = vdisplay + vfp + vsw + vbp */
	.vtotal = T1_HEIGHT + T1_VFP_F1 + T1_VSW_F1 + T1_VBP_F1,
	.vrefresh = 27,
};

static const struct panel_wtl_desc wtl_desc = {
	.modes = &wtl_mode,
	.num_modes = 1,
	.bpc = 6,
	.size = {
		.width = 69,
		.height = 123,
	},
	.flag = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE,
	.lanes = 2,
	.format = MIPI_DSI_FMT_RGB888,
};

static const struct of_device_id panel_wtl_of_match[] = {
	{
		.compatible = "wtl096601g01",
		.data = &wtl_desc,
	}, {
		/* sentinel */
	}
};
MODULE_DEVICE_TABLE(of, panel_wtl_of_match);

static int panel_wtl_pre_probe(struct mipi_dsi_device *dsi)
{
	const struct of_device_id *id;

	if (!of_device_is_available(dsi->dev.of_node))
		return -ENODEV;

	DRM_DEBUG_KMS("%s,%d\n", __FILE__, __LINE__);
	id = of_match_node(panel_wtl_of_match, dsi->dev.of_node);
	if (!id)
		return -ENODEV;

	return panel_wtl_probe(&dsi->dev, id->data);
}

static struct mipi_dsi_driver panel_wtl_dsi_driver = {
	.driver = {
		.name = "panel-wtl",
		.of_match_table = panel_wtl_of_match,
	},
	.probe = panel_wtl_pre_probe,
	.remove = panel_wtl_remove,
	.shutdown = panel_wtl_shutdown,
};

static int __init panel_wtl_init(void)
{
	int err;

	DRM_DEBUG_KMS("%s,%d\n", __FILE__, __LINE__);
	err = mipi_dsi_driver_register(&panel_wtl_dsi_driver);
	if (err < 0)
		return err;

	return 0;
}
module_init(panel_wtl_init);

static void __exit panel_wtl_exit(void)
{
	mipi_dsi_driver_unregister(&panel_wtl_dsi_driver);
}
module_exit(panel_wtl_exit);

MODULE_AUTHOR("lomboswer <lomboswer@lombotech.com>");
MODULE_DESCRIPTION("DRM Driver for Panels wtl096601g01");
MODULE_LICENSE("GPL and additional rights");
